package com.ht.medicaltreatment.utils

import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.gym.baselib.utils.runOnMain
import com.gym.baselib.utils.runOnThread
import com.rfid.api.ADReaderInterface
import com.rfid.api.GFunction
import com.rfid.def.ApiErrDefinition
import com.rfid.def.RfidDef
import com.xgst.lib.utils.TimeUtils

//手持盘点仪
object HandDevicesUitls {
    private var bGetScanRecordFlg = false
    private val conStr = "RDType=RPAN;CommType=BLUETOOTH;Name=R-PAN.35E998"
    private var mReader:ADReaderInterface? = null
    private var isOpen = false
    private val scanList = arrayListOf<String>()
    private var mListener:ScanListener? = null

    fun init(listener: ScanListener) {
        LogUtils.i("设备初始化。。。")
        mListener = listener
        if(mReader != null && isOpen){
            return
        }
        mReader = ADReaderInterface()
        open()
    }

    private fun open(success:()->Unit = {}){
        if(mReader?.RDR_Open(conStr) == ApiErrDefinition.NO_ERROR){
            isOpen = true
            success()
            LogUtils.i("设备打开成功")
        }else{
            isOpen = false
            LogUtils.i("手持盘点仪打开失败")
            ToastUtils.showShort("手持盘点仪打开失败")
        }
    }

    fun removeFilterUID(uid:String){
        TimeUtils.countDownOneTime(3000,{
            scanList.remove(uid)
        })
    }

    fun startScan(){
        if(!isOpen){
            open{
                scan()
            }
        }else{
            scan()
        }
    }

    private fun scan(){
        scanList.clear()
        runOnThread {
            var nret: Int?
            bGetScanRecordFlg = true
            var gFlg: Byte = 0x00 // 初次采集数据或者上一次采集数据失败时，标志位为0x00
            var dnhReport: Any?
            // 清空缓冲区记录
            nret = mReader?.RPAN_ClearScanRecord()
            if (nret != ApiErrDefinition.NO_ERROR) {
                bGetScanRecordFlg = false
                //盘点错误，结束
                ToastUtils.showShort("设备启动失败，正在重新启动...")
                TimeUtils.countDownOneTime(1200,{
                    startScan()
                })
                return@runOnThread
            }
            ToastUtils.showShort("设备启动成功")
            SoundUtils.playSound(SoundUtils.SOUND_SUCCESS)
            while (bGetScanRecordFlg) {
                nret = mReader?.RPAN_GetRecord(gFlg)
                if (nret != ApiErrDefinition.NO_ERROR) {
                    gFlg = 0x00
                    continue
                }
                gFlg = 0x01 // 数据获取成功，将标志位设置为0x01
                dnhReport = mReader?.RDR_GetTagDataReport(RfidDef.RFID_SEEK_FIRST)
                while (dnhReport != null) {
                    val byData: ByteArray? = mReader?.RPAN_ParseRecord(dnhReport)
                    val uid = GFunction.encodeHexStr(byData)
                    if(scanList.contains(uid)){
                        continue
                    }
                    scanList += uid
                    runOnMain {
                        mListener?.onScan(uid)
                    }
                    dnhReport = mReader?.RDR_GetTagDataReport(RfidDef.RFID_SEEK_NEXT)
                }
            }
            bGetScanRecordFlg = false
        }
    }

    fun stopScan(){
        bGetScanRecordFlg = false
    }

    fun close(){
        stopScan()
        isOpen = false
        mReader?.RDR_Close()
    }
}

interface ScanListener{
    fun onScan(uid:String)
}